// PREFS FUNCTIONS

/*
 *
 * Returns the prefs object to set and get prefs.
 *
 */

function pow_get_prefs_service() {
  var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                     .getService(Components.interfaces.nsIPrefBranch);
  return prefs;
}

/*
 *
 * Returns a large, almost unique, random number for ID purposes.
 *
 */

function pow_bc_get_random_number() {
  random_number = Math.floor(Math.random()*1000000000);
  return random_number;
}

/*
 *
 * Returns a random number to act as the password for transfers.
 *
 */

function pow_bc_get_unique_password() {
  try {
    var prefs = pow_get_prefs_service();
    if(prefs.getPrefType("pow.unique_password") == prefs.PREF_INT) {
      return prefs.getIntPref("pow.unique_password");
    } else {
      var rand = pow_bc_get_random_number();
      pow_set_unique_password(rand);
      return rand;
    } 
  } catch (e) {
    var rand = pow_bc_get_random_number();
    pow_set_unique_password(rand);
    return rand;
  }
}

/*
 *
 * Sets unique ID of POW.
 *
 */

function pow_bc_set_unique_userid(number) {
    var prefs = pow_get_prefs_service();
    prefs.setIntPref("pow.unique_userid", number);
}

/*
 *
 * Sets unique ID of POW. Only one of these should be here.
 *
 */

function pow_set_unique_password(number) {
    var prefs = pow_get_prefs_service();
    prefs.setIntPref("pow.unique_password", number);
}

/*
 *
 * Returns unique ID of POW. 
 *
 */

function pow_bc_get_unique_userid() {
  try {
    var prefs = pow_get_prefs_service();
    if(prefs.getPrefType("pow.unique_userid") == prefs.PREF_INT) {
      return prefs.getIntPref("pow.unique_userid");
    } else {
      var rand = pow_bc_get_random_number();
      pow_bc_set_unique_userid(rand);
      return rand;
    }
  } catch (e) {
    var rand = pow_bc_get_random_number();
    pow_bc_set_unique_userid(rand);
    return rand;
  }
}

/*
 *
 * Returns whether loopback-only (localhost connection only) is set.
 *
 */

function pow_bc_get_loopback_only() {
		try {
			var prefs = pow_get_prefs_service();
  	  if(prefs.getPrefType("pow.loopback_only.enabled") == prefs.PREF_BOOL)
   	     return prefs.getBoolPref("pow.loopback_only.enabled");
   	  else {
   	     prefs.setBoolPref("pow.loopback_only.enabled", false);
   	     return false;
   	  }
		} catch(e) {
			return false;
		}
}

/*
 *
 * Returns boolean if pass protected server that requires a login.
 *
 */

function pow_bc_pass_protected() {
	try {
		var prefs = pow_get_prefs_service();
 	  if(prefs.getPrefType("pow.pass_protected.enabled") == prefs.PREF_BOOL) {
  	     return prefs.getBoolPref("pow.pass_protected.enabled");
 	  } else {
  	     prefs.setBoolPref("pow.pass_protected.enabled", false);
  	     return false;
 	  }
	} catch(e) {
		return false;
	}
}

/*
 *
 * Returns whether server is on (1) or off (0).
 *
 */

function pow_get_icon_state() {
  try {
			var prefs = pow_get_prefs_service();
    if(prefs.getPrefType("pow.icon_state") == prefs.PREF_INT) {
      return prefs.getIntPref("pow.icon_state");
    } else {
      pow_set_icon_state(1);
      return 1;
    }
    return 1;
  } catch (e) {
    pow_set_icon_state(1);
    return 0;
  }
}

/*
 *
 * Sets whether server is already on (1) or off (0).
 *
 */

function pow_set_icon_state(state) {
	var prefs = pow_get_prefs_service();
  return prefs.setIntPref("pow.icon_state", state);
}

/*
 *
 * Returns authorization cookie.
 *
 */

function pow_bc_get_auth_cookie() {
  try {
    var prefs = pow_get_prefs_service();
    var date = new Date();
    var time = Math.floor(date.getTime()/1000);
    if(prefs.getPrefType("pow.auth_cookie_exp") == prefs.PREF_INT) {
      var exp = prefs.getIntPref("pow.auth_cookie_exp");
      if(exp < time) {
        return 0;
      }
    } else {
      time += 3600*24*30
      prefs.setIntPref("pow.auth_cookie_exp", time);
    }
    if(prefs.getPrefType("pow.auth_cookie") == prefs.PREF_INT)
      return prefs.getIntPref("pow.auth_cookie");
    else
      return 0;
  } catch (e) {
		log_error(e);
    return 0;
  }
}

/*
 *
 * Sets authorization cookie and date.
 *
 */

function pow_bc_set_auth_cookie(cookie) {
  try {
  var prefs = pow_get_prefs_service();
  var date = new Date();
  var time = Math.floor(date.getTime()/1000);
  time += 3600*24*30
  prefs.setIntPref("pow.auth_cookie_exp", time);
  return prefs.setIntPref("pow.auth_cookie", cookie);
	} catch (e) {
		log_error(e);
		return "";
	}
}

/*
 *
 * Returns port number of server.
 *
 */

function pow_bc_get_port_number() {
		try {
  		var prefs = pow_get_prefs_service();
   	 if(prefs.getPrefType("pow.port") == prefs.PREF_INT) {
	 	   return prefs.getIntPref("pow.port");
  		} else  if(navigator.userAgent.match(/(Firefox|Flock)/i)) {
				return 6670;
			} else if(navigator.userAgent.match(/Thunderbird/i)) {
  			return 6671;
			} else if(navigator.userAgent.match(/Sunbird/i)) {
  			return 6672;
			} else if(navigator.userAgent.match(/Pow/i)) {
  			return 6673;
			}
			// 6674 reserved for secure server
			return 6670;
		} catch (e) {
			if(navigator.userAgent.match(/(Firefox|Flock)/i)) {
				return 6670;
			} else if(navigator.userAgent.match(/Thunderbird/i)) {
  			return 6671;
			} else if(navigator.userAgent.match(/Sunbird/i)) {
  			return 6672;
			} else if(navigator.userAgent.match(/Pow/i)) {
  			return 6673;
			}
			return 6670;
		}
}

/*
 *
 *
 */

function pow_set_pass_protected(enabled) {
  		var prefs = pow_get_prefs_service();
    return prefs.setBoolPref("pow.pass_protected.enabled", enabled);
}

function pow_set_loopback_only(enabled) {
  		var prefs = pow_get_prefs_service();
    return prefs.setBoolPref("pow.loopback_only.enabled", enabled);
}

function pow_get_mimelist_pref() {
	try {
 		var prefs = pow_get_prefs_service();

    if(prefs.getPrefType("pow.mimelist") == prefs.PREF_STRING) {
	    return prefs.getCharPref("pow.mimelist");
    } else if(prefs.getPrefType("pow.blacklist") == prefs.PREF_STRING) {
			// REMOVE AFTER UPGRADE
	    var black = prefs.getCharPref("pow.blacklist");
			pow_set_mimelist_pref(black);
			return black;
		} else {
			var prefs = pow_get_default_mimes();
			pow_set_mimelist_pref(prefs);
			return prefs;
		}
	} catch (e) {
		log_error(e);
		// html etc
		var prefs = pow_get_default_mimes();
		pow_set_mimelist_pref(prefs);
		return prefs;
	}
}

function pow_bc_get_login() {
	try {
 		var prefs = pow_get_prefs_service();

    if(prefs.getPrefType("pow.login") == prefs.PREF_STRING)
	    return prefs.getCharPref("pow.login");
	else
		return "";
	} catch(e) {
		return "";
	}
}

// Why do we need 2 of these?
function pow_get_login() {
	try {
 		var prefs = pow_get_prefs_service();

    if(prefs.getPrefType("pow.login") == prefs.PREF_STRING)
	    return prefs.getCharPref("pow.login");
	else
		return "";
	} catch(e) {
		return "";
	}
}

function pow_bc_get_password() {
	try {
 		var prefs = pow_get_prefs_service();

    if(prefs.getPrefType("pow.password") == prefs.PREF_STRING)
	    return prefs.getCharPref("pow.password");
	else
		return "";
	} catch(e) {
		return "";
	}
}

function pow_bc_get_breadcrumbs_mime_list() {
	var types = get_mime_types();
	return types;
}

function get_pow_root_dir() {
	try {
 		var prefs = pow_get_prefs_service();

    if(prefs.getPrefType("pow.root_directory") == prefs.PREF_STRING) {
	    var root = prefs.getCharPref("pow.root_directory");
			if(!root) {
				return "";
			} else {
	    	return root;
			}
		} else {
			return "";
		}
	} catch(e) {
		return "";
	}
}

function get_mime_types() {
	try {
		this.mime_pref =  pow_get_mimelist_pref();
		if(!this.mime_pref) {
			this.mime_pref =  pow_get_default_mimes();
			pow_set_mimelist_pref(this.mime_pref);
		}
		return this.mime_pref;
	} catch (e) {
		log_error(e);
	}
	return "";
}

function pow_bc_get_root_directory() {
  return "/pow/htdocs";
}

function pow_bc_get_data_directory() {
  return "/pow/data";
}

function pow_bc_get_log_directory() {
  return "/pow/log";
}

function pow_bc_get_index_directory() {
  return "/pow";
}

function get_absolute_root_directory() {
    prefs.setIntPref("pow.root_directory", root_dir);
}

function pow_get_default_mimes() {
	return "sjs,text/html,false,"+
			"pl,text/html,false,"+
			"php,text/html,false,"+
			"rb,text/html,false,"+
			"py,text/html,false";
}

function pow_get_binary_from_extension(extension) {
	var ascii_types = "txt php tex latex rtf js css csv html htm xml";
	var asciis = ascii_types.split(" ");
	for(var i in asciis) {
		if(extension == asciis[i]) {
			return false;
		}
	}
	return true;
}

// Set the pow.blacklist pref to the given string
function pow_set_blacklist_pref(prefStr) {
 		var prefs = pow_get_prefs_service();

    prefs.setCharPref("pow.blacklist", prefStr);
}

function pow_set_mimelist_pref(prefStr) {
 		var prefs = pow_get_prefs_service();
    prefs.setCharPref("pow.mimelist", prefStr);
}

function pow_set_login(prefStr) {
 		var prefs = pow_get_prefs_service();
    prefs.setCharPref("pow.login", prefStr);
}

function pow_set_password(prefStr) {
		pow_set_string_pref("pow.password", prefStr);
}

function pow_set_string_pref(prefName, prefStr) {
 		var prefs = pow_get_prefs_service();
    prefs.setCharPref(prefName, prefStr);
}

function pow_bc_set_port_pref(portNum) {
 		var prefs = pow_get_prefs_service();
    prefs.setIntPref("pow.port", portNum);
}

function pow_set_root_dir(prefStr) {
	try {
		if(prefStr != "" && !prefStr) { return; }
			pow_set_string_pref("pow.root_directory", prefStr);
	} catch(e) {
		log_error(e);
	}
}
